/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.text.html.HTMLDocument;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import protocol.MetaData;

/**
 *
 * @author Acer
 */
public class Utility {
    
    public static final int SERVER_PORT     = 4444;
    public static final String SERVER_HOST  = "192.168.1.102";
    public static final int SERVER_TIMEOUT  = 300000;
    public static final String SERVER_REPO  = "repositories";
    public static final long SYNC_PERIOD    = 10000;

    public static final byte TYPE_FILE = 0;
    public static final byte TYPE_FOLDER = 1;
    public static final byte TYPE_DELETED = 2;
    /**
     * Create a new directory with full name file path
     * @param filepath 
     */
    public static boolean CreateNewDirectory(String filepath){
        boolean success = (new File(filepath)).mkdir();
        return success;
    }
    
    /**
     * Memindahkan folder dari absolutepath1 ke absolutepath2
     * @param absolutepath1
     * @param absolutepath2
     * @return 
     */
    public static boolean MoveDirectory(String absolutepath1,String absolutepath2){
        try {
            File initialdirectory = new File(absolutepath1);
            File resultdirectory = new File(absolutepath2);
            FileUtils.moveDirectoryToDirectory(initialdirectory, resultdirectory, true);
            System.out.println("Successfully moving directory");
        } catch (Exception e) {
            System.out.println("Failed moving directory");
            return false;
        }
        return true;
    }
    public static boolean DeleteDirectory(String absolutepath){
        try {
            File directory = new File(absolutepath);
            FileUtils.deleteDirectory(directory);
            System.out.println("Directory succesfully deleted");
        } catch (Exception e) {
            System.out.println("Failed in deleting directory");
            return false;
        }
        return true;
    }
    public static boolean DeleteFile(String absulotepath){
        try {
            File file = new File(absulotepath);
            FileUtils.deleteQuietly(file);
            System.out.println("Success in deleting file");
        } catch (Exception e) {
            System.out.println("Failed in deleting file");
            return false;
        }
        return true;
    }
    
    public static String convertToSHA1(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        return convertToSHA1(input.getBytes("iso-8859-1"));
    }
    
    public static String convertToSHA1(byte[] byteFile) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(byteFile);
        byte[] sha1hash = md.digest();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < sha1hash.length; i++) {
          sb.append(Integer.toString((sha1hash[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
    public static byte[] getFileByte(String path) throws IOException {
        File file = new File(path);
        if (file.isDirectory())
            return new byte[0];
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
        StringBuilder sb = new StringBuilder();
        int read;
        while ((read = bis.read()) != -1)
            sb.append((char)read);
        String myByte = sb.toString();
        byte[] res = new byte[myByte.length()];

        for (int i = 0; i < res.length; ++i)
            res[i] = (byte)myByte.charAt(i);
        bis.close();
        return res;
    }
    
    public static ArrayList<MetaData> getMetaDataFromFolder(String absolutepathfolder){
        ArrayList<MetaData> arrMetaData = new ArrayList<MetaData>();
        File root = new File(absolutepathfolder);
        try {
            boolean recursive = true;
//            Collection files = FileUtils.listFiles(root, null, recursive);
            Collection files = FileUtils.listFilesAndDirs(root, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
//            System.out.println("Folder root : " + root);
            for(Iterator iterator = files.iterator();iterator.hasNext();){
//                System.out.println("Iterasi folder");
                File file = (File)iterator.next();
//                System.out.println("File : " + file.getAbsolutePath());
                //System.out.println(absolutepathfolder);
                absolutepathfolder = absolutepathfolder.replace("/", "\\");
                if(!absolutepathfolder.equals(file.getAbsolutePath()))
                arrMetaData.add(MetaData.getFileMetaData(absolutepathfolder, file.getAbsolutePath()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Menghilangkan MetaData ke-0
//        arrMetaData.remove(0);
        return arrMetaData;
    }
    
    public static void PerformArrayListMetaData(ArrayList<MetaData> arrMetaData){
        int i;
        for(i=0;i<arrMetaData.size();++i){
            MetaData.PerformMetaData(arrMetaData.get(i));
        }
    }
    
    public static void main(String[] args){
        String abspath1 = "D:\\8box";
        String abspath2 = "D:\\8box2";
//        MoveDirectory(abspath1, abspath2);
//        DeleteFile("D:\\8box2\\boolean.h");
        ArrayList<MetaData> arrMetaData = getMetaDataFromFolder(abspath2);
        int i;
        for(i=0;i<arrMetaData.size();++i){
            arrMetaData.get(i).PerformMetaData();
        }
    }
}

